import 'package:flutter/material.dart';
import 'package:necproject/providers/user_provider.dart';
import 'package:provider/provider.dart';

class MyDrawer extends StatefulWidget {
  MyDrawer({Key? key}) : super(key: key);

  @override
  _MyDrawerState createState() => _MyDrawerState();
}

class _MyDrawerState extends State<MyDrawer> {
  @override
  void initState() {
    super.initState();
    //注册一个回调函数yourCallback
    WidgetsBinding.instance?.addPostFrameCallback((_) => yourCallback(context));
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      width:
          MediaQuery.of(context).size.width > MediaQuery.of(context).size.height
              ? MediaQuery.of(context).size.width * 0.2
              : MediaQuery.of(context).size.width *
                  0.7, // 75% of screen will be occupied,
      // height: MediaQuery.of(context).size.height,
      color: Theme.of(context).primaryColor,
      child: Drawer(
          child: ListView(
        children: [
          Column(
            children: <Widget>[
              Row(
                children: <Widget>[
                  Expanded(
                    child: UserAccountsDrawerHeader(
                      accountName: Text(
                          Provider.of<UserProvider>(context).isLogin
                              ? Provider.of<UserProvider>(context)
                                  .userInfo['username']
                                  .toString()
                              : '未登录游客'),
                      accountEmail: Text(
                          Provider.of<UserProvider>(context).isLogin
                              ? Provider.of<UserProvider>(context)
                                  .userInfo['email']
                                  .toString()
                              : 'unknownpeople@unknown.com'),
                      currentAccountPicture: CircleAvatar(
                        backgroundImage: NetworkImage(
                          Provider.of<UserProvider>(context).isLogin
                              ? Provider.of<UserProvider>(context)
                                  .userInfo['avatar']
                                  .toString()
                              : 'https://www.itying.com/images/flutter/3.png',
                        ),
                      ), //用户头像
                      decoration: BoxDecoration(
                        image: DecorationImage(
                          image: NetworkImage(
                              'https://www.itying.com/images/flutter/2.png'),
                          fit: BoxFit.cover,
                        ),
                      ), //背景
                      otherAccountsPictures: <Widget>[
                        Image.network(
                            'https://www.itying.com/images/flutter/4.png'),
                        Image.network(
                            'https://www.itying.com/images/flutter/5.png'),
                        Text('data')
                      ],
                    ),
                  ),
                ],
              ),
              ListTile(
                leading: CircleAvatar(
                  child: Icon(Icons.home),
                ),
                title: Text('我的空间'),
                onTap: () {
                  Navigator.of(context).pop(); //隐藏侧边栏
                  Navigator.pushNamed(context, '/user');
                },
              ),
              Divider(), // 增加一条线
              ListTile(
                leading: CircleAvatar(
                  child: Icon(Icons.people),
                ),
                title: Text('用户中心'),
              ),
              Divider(), // 增加一条线
              ListTile(
                leading: CircleAvatar(
                  child: Icon(Icons.settings),
                ),
                title: Text('设置'),
              ),
            ],
          ),
        ],
      )),
    );
  }

  yourCallback(BuildContext context) {
    print('333333');
  }
}
